<?php

namespace Deployer;

require 'recipe/common.php';

// Project name
set('application', 'lottery');

// Project repository
// set('repository', 'git@47.107.73.162:wendidi/gysa_official_web.git');

// [Optional] Allocate tty for git clone. Default value is false.
set('git_tty', false);

// Shared files/dirs between deploys
set('shared_files', []);
set('shared_dirs', []);

// Writable dirs by web server
set('writable_dirs', []);

// Hosts
host('lottery')
    ->hostname('39.99.242.153')
	->set('repository', 'git@47.107.73.162:activities/lottery.git')
    ->set('branch', 'master')
    ->set('deploy_path', '/var/www/web.vodeshop.com/{{application}}')
    ->user('deployer')
    ->identityFile('~/.ssh/deployerkey')
    ->forwardAgent(true)
	->multiplexing(false)
	->addSshOption('UserKnownHostsFile', '/dev/null')
	->addSshOption('StrictHostKeyChecking', 'no');


// Tasks
desc('Execute chattr -i /path/to/.user.ini');
task('cleanup:user-ini', function () {
    $releases = get('releases_list');
    $keep     = get('keep_releases');

    if ($keep === -1) {
        // Keep unlimited releases.
        return;
    }

    while ($keep > 0) {
        array_shift($releases);
        --$keep;
    }

    foreach ($releases as $release) {
        if (test("[ -f {{deploy_path}}/releases/$release/.user.ini ]")) {
            run("chattr -i {{deploy_path}}/releases/$release/.user.ini");
        }
    }
});

desc('Execute chattr -i /path/to/.user.ini');
task('rollback:user-ini', function () {
    $releases = get('releases_list');
    if (isset($releases[1]) && test("[ -f {{deploy_path}}/releases/{$releases[0]}/.user.ini ]")) {
        // Remove release
        run("chattr -i {{deploy_path}}/releases/{$releases[0]}/.user.ini");
    }
});




desc('run yarn');
task('yarn:install', function () {
//   run("cd {{release_path}} && yarn");
});

desc('run yarn build');
task('yarn:build', function () {
//    run("cd {{release_path}} && yarn build");
});




desc('Deploy your project');
task('deploy', [
    'deploy:info',
    'deploy:prepare',
    'deploy:lock',
    'deploy:release',
    'deploy:update_code',
	'deploy:shared',

	'yarn:install',
	'yarn:build',

	'deploy:symlink',

    'deploy:unlock',
    'cleanup:user-ini',
    'cleanup',
    'success'
]);

// [Optional] If deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');

after('rollback', 'rollback:user-ini');
